using System;
using System.Web.UI;

using LocalBoard.Domain;
using LocalBoard.Controller;

namespace LocalBoard.Web
{
	public class MessageUserControl : BoardUserControl
	{
		protected MessageController messageController
		{
			get
			{
				if(_currentMessage != null)
					return new MessageController(_currentMessage, Global.AccessorFactory);
				else
					return new MessageController(_currentMessageId, Global.AccessorFactory);
			}
		}
		protected string CurrentBoardPageUrl
		{
			get{return ((BoardPage)Page).CurrentBoardPageUrl;}
		}
		protected int _currentMessageId = 0;
		protected int _replyToMessageId = 0;
		protected string _messageViewMode = "view";
		protected Message _replyToMessage = null;
		protected Message _currentMessage = null;
		public Message CurrentMessage
		{
			get{return _currentMessage;}
		}

		public int GetMessageId()
		{
			int messageNo = 0;
			try
			{
				messageNo = Convert.ToInt32(Request.Params["message"]);
			}
			catch
			{}
			return messageNo;
		}

		public int GetReplyToMessageId()
		{
			int replyToNo = 0;
			try
			{
				replyToNo = Convert.ToInt32(Request.Params["replyto"]);
			}
			catch
			{}
			return replyToNo;
		}

		public string GetMessageViewMode()
		{
			return (Request.Params["mode"] == null)?"":Request.Params["mode"];
		}

		protected void SetMessageId(int messageId)
		{
			_currentMessageId = messageId;
		}
		protected void SetReplyToMessageId(int messageId)
		{
			_replyToMessageId = messageId;
		}
		protected void SetMessageViewMode(string mode)
		{
			_messageViewMode = mode;
		}

		protected bool LoadCurrentMessage()
		{
			if(_currentMessageId > 0)
			{
				_currentMessage = boardController.GetMessage(_currentMessageId);
				return true;
			}
			else
				return false;
			//				throw new Exception("Message Id has to be assigned to use MessageView.ascx control");
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			if(GetMessageViewMode().Length > 0)
				SetMessageViewMode(GetMessageViewMode());
			SetMessageId(GetMessageId());
			SetReplyToMessageId(GetReplyToMessageId());
			base.OnInit(e);
		}
		#endregion
	}
}